package com.dhgate.spu.analysis.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.standard.StandardFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.synonym.SolrSynonymParser;
import org.apache.lucene.analysis.synonym.SynonymFilter;
import org.apache.lucene.analysis.synonym.SynonymMap;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhgate.spu.analysis.consts.SysConsts;
/** 
 * @author:      wangguodong 
 * @create:      2014-3-14 下午3:57:33 
 * @description: 
 */
public class SpuAnalyzer extends Analyzer{
	private static SynonymMap SYNONYM_MAP = null;
	private static final Logger logger = LoggerFactory.getLogger(SpuAnalyzer.class);
	static {
		boolean dedup = true;
		boolean expand = false;
		SolrSynonymParser parser = new SolrSynonymParser(dedup, expand, new StandardAnalyzer(Version.LUCENE_43));
		try {
			parser.add(new FileReader(new File(SysConsts.CONST_SYNOYM_FILE_PATH)));
			SYNONYM_MAP = parser.build();
		} catch (Exception e) {
			logger.error(e+"");
			System.out.println(e);
			e.printStackTrace();
		}
	}
	
	@Override
	protected TokenStreamComponents createComponents(String fieldName,Reader reader) {
		final  StandardTokenizer tokenizer = new StandardTokenizer(Version.LUCENE_43, reader);
	    TokenStream stream = new StandardFilter(Version.LUCENE_43, tokenizer);
	    stream = new LowerCaseFilter(Version.LUCENE_43, stream); 
	    stream = new StopFilter(Version.LUCENE_43, stream, StopAnalyzer.ENGLISH_STOP_WORDS_SET);
	    stream  = new SynonymFilter(stream, SYNONYM_MAP, true);
	    return new TokenStreamComponents(tokenizer, stream);
	}
	
}
